package it.eng.area118.mdogpscollector.service;

import it.eng.area118.mdogpscollector.db.DbManager;
import it.eng.area118.mdogpscollector.model.CheckPoint;
import it.eng.area118.mdogpscollector.model.PerstRootBean;

import org.garret.perst.Index;
import org.garret.perst.Storage;

import android.os.Environment;

public class CheckPointLookupService {

	private static IndexLookupService service;

	public synchronized static IndexLookupService getService() {
		if (service == null) {
			String basePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
			Storage db = DbManager.getDb(basePath + "/db.dbs");
			PerstRootBean root = (PerstRootBean) db.getRoot();
			Index i = root.getIndex(CheckPoint.class, db, false);
			service = new IndexLookupService(i, CheckPoint.class, db);
		}
		return service;
	}

	public synchronized static void reset() {
		String basePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
		Storage db = DbManager.getDb(basePath + "/db.dbs");
		service.reset(CheckPoint.class, db);
	}
}
